package com.itdct.onflow.system.generator.core

import com.itdct.onflow.com.itdct.onflow.system.generator.config.SysGeneratorConfig
import com.itdct.onflow.com.itdct.onflow.system.generator.context.SysGeneratorContext
import com.itdct.onflow.com.itdct.onflow.system.generator.util.JdbcHelper
import com.itdct.onflow.core.extend.logInfo
import com.itdct.onflow.generator.model.FieldModel
import com.itdct.onflow.generator.util.GeneratorLogUtil
import com.itdct.onflow.system.api.entity.SysApiEntity
import java.sql.Connection

/**
 * @author Zhouwx
 * @date 2025/9/8 17:02:41
 * @version 1.0
 * @description
 */
class SyncApiCore {
    fun syncApi(context: SysGeneratorContext, generatorConfig: SysGeneratorConfig) {
        val connection = context.connection ?: throw RuntimeException("请先初始化数据库连接")
        val classModel = context.classModel

        insertApi(context, generatorConfig, connection, "add")
        insertApi(context, generatorConfig, connection, "delete")
        insertApi(context, generatorConfig, connection, "update")
        insertApi(context, generatorConfig, connection, "page")
        insertApi(context, generatorConfig, connection, "detail")

        // INFO: Zhouwx: 2025/9/8 下拉选接口入库
        classModel.fieldList
            .filter { it.forSelect }
            .forEach {
                insertApi(context, generatorConfig, connection, "select", it)
            }
    }

    fun insertApi(context: SysGeneratorContext, generatorConfig: SysGeneratorConfig, connection: Connection, type: String, fieldModel: FieldModel? = null) {
        val classModel = context.classModel

        val typeName = when (type) {
            "page" -> "列表"
            "add" -> "添加"
            "update" -> "更新"
            "delete" -> "删除"
            "detail" -> "详情"
            "select" -> "下拉选"
            else -> throw RuntimeException("未知类型")
        }
        val sysApiEntity = SysApiEntity()
        sysApiEntity.platformType = generatorConfig.platformType
        var url = ""
        if (generatorConfig.contextPath.isNotBlank()) {
            url = generatorConfig.contextPath
        }
        if (fieldModel != null) {
            // INFO: Zhouwx: 2025/9/8 说明是下拉选接口
            url += "/" + classModel.classNameLowCamel + "/select${fieldModel.nameBigCamel}List" + "." + generatorConfig.apiSuffix
            sysApiEntity.name = fieldModel.comment + "下拉选"
        } else {
            url += "/" + classModel.classNameLowCamel + "/" + type + "." + generatorConfig.apiSuffix
            sysApiEntity.name = classModel.classUsage + typeName
        }

        val sysApiEntities = JdbcHelper.selectList(connection, context.sysApiClassModel, Pair(SysApiEntity::url, url))
        if (sysApiEntities.isNotEmpty()) {
            logInfo("已存在${classModel.classUsage}${typeName}接口，URL为：$url")
            return
        }

        sysApiEntity.url = url
        sysApiEntity.controlType = generatorConfig.apiDefaultControlType
        sysApiEntity.method = "POST"
        sysApiEntity.moduleName = classModel.classUsage
        sysApiEntity.enable = true
        val id = JdbcHelper.insert(connection, context.sysApiClassModel, sysApiEntity, {
            GeneratorLogUtil.appendSql(it, context, generatorConfig)
            it
        })
        sysApiEntity.id = id
    }
}